/**
 * ===========================================
 * Project: qb-common
 * ===========================================
 * Package: com.cyou.qb.common.service.impl
 * 
 * Copyright (c) 2012, CYOU All Rights Reserved.
 * ===========================================
 */
package com.cyou.qb.common.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.cyou.qb.common.dao.AssignedWinnerDao;
import com.cyou.qb.common.dao.AwardDao;
import com.cyou.qb.common.domain.AssignedWinner;
import com.cyou.qb.common.domain.Award;
import com.cyou.qb.common.service.AwardConfigChanceService;
import com.cyou.qb.exception.ServiceException;

/**
 * <p>AwardConfigChanceImpl</p>
 * <p>
 * Description:配置中奖方式service实现
 * </p>
 *
 * @author lidongwei
 * @version 
 * @since 2012-10-11
 */
@Service("AwardConfigChanceImpl")
public class AwardConfigChanceServiceImpl extends BaseSerivceImpl implements AwardConfigChanceService {

	@Autowired
	AssignedWinnerDao assignedWinnerDao;
	@Autowired
	private AwardDao awardDao;

	/**
	 * Description 根据奖品id获取制定中奖用户
	 * @author lidongwei
	 * @since 2012-10-11 下午1:52:49
	 * @see TODOsome class
	 */
	@Override
	public List<AssignedWinner> findAssignedWinnerListByAwardId(String awardId) {

		return assignedWinnerDao.findAssignedWinnerbyAwardId(Integer.valueOf(awardId));
	}

	/**
	 * Description
	 * @author lidongwei
	 * @throws Exception 
	 * @since 2012-10-11 下午8:09:21
	 * @see TODOsome class
	 */
	@Override
	public void saveAwardConfigChance(String awardId, String probabilityCheckBox, String prizeRadio,
			String prizeTimeInput, String prizeNumInput) throws Exception {
		Award award = awardDao.findAwardById(Integer.valueOf(awardId));
		if (award != null) {
			Award oldAw = award;
			if (probabilityCheckBox.equals("on")) {
				if (prizeRadio.equals("3")) {//选中尾数中奖
					award.setWinType(3);
					Assert.isTrue(StringUtils.isNumeric(prizeNumInput), "获取尾数格式错误");
					//根据活动id和尾数获取此活动中此尾数的奖品的数量，>0则不可添加
					int count = awardDao.findAwardCountByNumInput(Integer.valueOf(prizeNumInput), award.getActiveId(),
							award.getId());
					if (count <= 0) {
						award.setNumWin(Integer.valueOf(prizeNumInput));
						award.setDateWin("");
					} else {
						throw new ServiceException("不能再同一个活动中添加相同的尾数");
					}

				} else if (prizeRadio.equals("2")) {//选中时间中奖
					award.setWinType(2);
					award.setDateWin(prizeTimeInput);
					award.setNumWin(-1);
				}

			} else {
				award.setWinType(0);
				award.setNumWin(-1);
				award.setDateWin("");
			}
			awardDao.updateOneAward(award);
			getServiceContext().addLog(award.getName(), award.getId(), "t_award", "添加中奖方式", new Object[] { award },
					new Object[] { oldAw });
		} else {
			throw new ServiceException("数据库出错,找不到奖品");
		}

	}

	/**
	 * Description 添加指定用户
	 * @author lidongwei
	 * @throws Exception 
	 * @since 2012-10-11 下午8:44:42
	 * @see TODOsome class
	 */
	@Override
	public AssignedWinner saveOneAssignWinner(String awardId, String passport, String num) throws Exception {
		Award award = awardDao.findAwardById(Integer.valueOf(awardId));
		if (award != null) {
			if (award.getCurrNum() < Integer.valueOf(num)) {
				throw new ServiceException("分配奖品数大于当前库存数");
			}
			AssignedWinner assignWinner = new AssignedWinner();
			assignWinner.setActiveId(award.getActiveId());
			assignWinner.setAwardId(Integer.valueOf(awardId));
			assignWinner.setCreateDate(new Date());
			assignWinner.setCurrNum(Integer.valueOf(num));
			assignWinner.setMaxNum(Integer.valueOf(num));
			assignWinner.setNickname("");
			assignWinner.setPassport(passport);
			assignWinner.setuCenterId(-1);
			assignedWinnerDao.saveOneAssignedWinner(assignWinner);
			getServiceContext().addLog(assignWinner.getPassport(), assignWinner.getId(), "t_assign_winner", "添加指定中奖用户",
					new Object[] { assignWinner }, null);
			int oldCurr = award.getCurrNum();
			award.setCurrNum(award.getCurrNum() - Integer.valueOf(num));
			awardDao.updateOneAward(award);
			getServiceContext().addLog(award.getName(), award.getId(), "t_award", "添加指定中奖用户，修改奖品当前数量",
					new Object[] { award.getCurrNum() }, new Object[] { oldCurr });
			return assignWinner;
		} else {
			throw new ServiceException("数据库出错,找不到奖品");
		}
	}

	/**
	 * Description
	 * @author lidongwei
	 * @since 2012-10-11 下午9:55:46
	 * @see TODOsome class
	 */
	@Override
	public boolean findPassport(String passport, String awardId) {

		int count = assignedWinnerDao.findAssignedWinnerByPassportAndAwardId(Integer.valueOf(awardId), passport);
		if (count == 0) {
			return false;
		}
		return true;
	}

	/**
	 * Description
	 * @author lidongwei
	 * @throws Exception 
	 * @since 2012-10-11 下午10:11:36
	 * @see TODOsome class
	 */
	@Override
	public void removeOneAssignWinner(String assignedWinnerId) throws Exception {
		AssignedWinner assignWinner = assignedWinnerDao.findAssignWinnerById(Integer.valueOf(assignedWinnerId));
		if (assignWinner.getCurrNum() != assignWinner.getMaxNum()) {//已经中奖
			throw new ServiceException("此用户已经中奖,不能被删除");

		} else {
			int awardId = assignWinner.getAwardId();
			Award award = awardDao.findAwardById(Integer.valueOf(awardId));
			if (award != null) {
				getServiceContext().addLog(assignedWinnerId, Integer.valueOf(assignedWinnerId), "t_assign_winner",
						"删除指定中奖用户", null,
						new Object[] { assignedWinnerDao.findAssignWinnerById(Integer.valueOf(assignedWinnerId)) });
				assignedWinnerDao.removeOneAssignWinner(Integer.valueOf(assignedWinnerId));
				//将奖品填回原处
				int oldCurr = award.getCurrNum();
				award.setCurrNum(award.getCurrNum() + assignWinner.getCurrNum());
				awardDao.updateOneAward(award);
				getServiceContext().addLog(award.getName(), award.getId(), "t_award", "删除指定中奖用户，修改奖品当前数量",
						new Object[] { award.getCurrNum() }, new Object[] { oldCurr });
			} else {
				throw new ServiceException("数据库出错,找不到奖品");
			}

		}

	}

	@Override
	public List<AssignedWinner> findAssignedWinner(int activeId, String passport) {
		return assignedWinnerDao.findAssignedWinner(activeId, passport);
	}

	@Override
	public void updateAssignedWinnerDrawTimes(int activeId, int awardId, String passport) {
		assignedWinnerDao.minusWinnerDrawTimes(activeId, awardId, passport);
	}

	@Override
	public List<AssignedWinner> findAllValidAssignedWinner() {
		return assignedWinnerDao.findAllValidAssignedWinner();
	}
}
